import time
import requests
import pymysql


# 获取数据库连接
def get_db_connection():
    return pymysql.connect(
        host="localhost",
        user="root",  # 替换为你的 MySQL 用户名
        password="17884873770",  # 替换为你的 MySQL 密码
        database="spider01",  # 数据库名称
        charset="utf8mb4"
    )


# 保存数据到 MySQL
def save_data_to_mysql(item):
    connection = get_db_connection()
    try:
        with connection.cursor() as cursor:
            sql = """
                INSERT INTO tieba_forum (forum_id, forum_name, avatar, thread_count, member_count)
                VALUES (%s, %s, %s, %s, %s)
                ON DUPLICATE KEY UPDATE
                thread_count = VALUES(thread_count),
                member_count = VALUES(member_count),
                avatar = VALUES(avatar)
            """
            cursor.execute(sql, (
                item["forum_id"],
                item["forum_name"],
                item.get("avatar", ""),
                item["thread_count"],
                item["member_count"]
            ))
        connection.commit()
    except Exception as e:
        print(f"保存数据时发生错误: {e}")
    finally:
        connection.close()


# 获取数据
def fetch_data(page):
    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Referer": "https://tieba.baidu.com/index.html",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
    }
    cookies = {
        # 替换为实际的 Cookies 信息
        "BIDUPSID": "example_bid",
        "BDUSS": "example_bduss",
    }
    url = "https://tieba.baidu.com/f/index/rcmdForum"
    params = {"pn": str(page), "rn": "12"}

    try:
        response = requests.get(url, headers=headers, cookies=cookies, params=params)
        response.raise_for_status()  # 检查请求是否成功
        data = response.json()

        for item in data["data"]["forum_info"]:
            print(f"正在保存: {item['forum_name']}")
            save_data_to_mysql(item)
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")
    except KeyError as e:
        print(f"解析数据失败: {e}")


if __name__ == '__main__':
    for page in range(1, 18):
        fetch_data(page)
        time.sleep(1)
